package com.shop.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.shop.entity.*;
import com.shop.shop.mapper.CommodityMapper;
import com.shop.shop.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop.utile.DateUtile;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.PageUtile;
import com.shop.shop.utile.Q;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ImageService imageService;
    @Autowired
    private SortService sortService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private IntroductionService introductionService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private SpecificationsService specificationsService;


    @Override
    public Map<String, Object> findByAllTop(MapUtile map) {
        Map<String, Object> masp = new HashMap<>();
        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        wrapper.eq("cm_top", 1);
        Object key = map.getKey();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("cm_name", key);
        }
        //判断调用的方法是不是秒杀的接口
        if (map.getSpike() == 1 && map.getSpike() != null) {
            wrapper.eq("cm_spike", map.getSpike());
        }
        Page<Commodity> page = new PageUtile<Commodity>().getPage(map);
        baseMapper.selectPage(page, wrapper);
        List<Commodity> list = page.getRecords();
        if (map.getSpike() == 1) {
            // 秒杀商品时间
            for (Commodity commodity : list) {
                Date cmSpikeTime = commodity.getCmSpikeTime();
                if (cmSpikeTime != null) {
                    Long time = DateUtile.getTime(cmSpikeTime);
                    if (time >= 0) {
                        //判断当前商品的秒杀时间是不是已经过了没有过
                        commodity.setSs(time);
                        // redisTemplate.opsForValue().set("id_"+commodity.getCmId(),commodity.getSs());
                        commodity.setSpikeString("秒杀中");
                    }
                }
                commodity.setSpikeString("秒杀结束");
            }

        }
        masp.put("list", list);
        masp.put("pages", page.getPages());
        masp.put("size", page.getSize());
        masp.put("page", page.getCurrent());
        masp.put("total", page.getTotal());
        return masp;
    }

    /**
     * id查询商品的信息：  商品信息 +分类+商家
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById(Integer id) {
        //id查询商品
        Map<String, Object> map = new HashMap<>();
        Commodity commodity = baseMapper.selectById(id);
        if(commodity.getCmSpikeTime()!=null){
            Long time = DateUtile.getTime(commodity.getCmSpikeTime());
            String timeString=time>0?"秒杀中":"秒杀结束";
            commodity.setSpikeString(timeString);
        }
        map.put("comm", commodity);

        //商品id查询  轮播图片
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("cm_id", commodity.getCmId());
        List<Image> images = imageService.list(imageQueryWrapper);
        map.put("images", images);

        //查询商品当前分类
        QueryWrapper<Sort> sortQueryWrapper = new QueryWrapper<>();
        sortQueryWrapper.eq("st_id", commodity.getStId());
        Sort sort = sortService.getOne(sortQueryWrapper);
        map.put("sort", sort);

        //查询当前商家
        QueryWrapper<Merchant> merchantQueryWrapper = new QueryWrapper<>();
        merchantQueryWrapper.eq("mt_id", commodity.getMtId());
        Merchant merchant = merchantService.getOne(merchantQueryWrapper);
        map.put("merchant", merchant);

        //查询商品的介绍
        QueryWrapper<Introduction> introductionQueryWrapper = new QueryWrapper<>();
        introductionQueryWrapper.eq("in_id", commodity.getCmInId());
        Introduction introduction = introductionService.getOne(introductionQueryWrapper);
        map.put("in", introduction);

        //id查询商品的规格参数
        QueryWrapper<Specifications> specificationsQueryWrapper=new QueryWrapper<>();
        specificationsQueryWrapper.eq("cm_id",commodity.getCmId());
        List<Specifications> list = specificationsService.list(specificationsQueryWrapper);
        map.put("spes",list);

        return map;
    }

    @Override
    public Map<String, Object> listUser(MapUtile map) {
        Map<String, Object> masp = new HashMap<>();
        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        QueryWrapper<Collect> collectQueryWrapper=new QueryWrapper<>();
        collectQueryWrapper.eq("ur_id",map.getUserId());
        List<Collect> list1 = collectService.list(collectQueryWrapper);
        for (Collect collect : list1) {
             wrapper.eq("cm_id",collect.getCmId()).or();
        }
        Object key = map.getKey();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("cm_name", key);
        }

        Page<Commodity> page = new PageUtile<Commodity>().getPage(map);
        baseMapper.selectPage(page, wrapper);
        List<Commodity> list = page.getRecords();
        masp.put("list", list);
        masp.put("pages", page.getPages());
        masp.put("size", page.getSize());
        masp.put("page", page.getCurrent());
        masp.put("total", page.getTotal());
        return masp;
    }

    @Override
    public Map<String, Object> findSeartop(MapUtile map) {
        Map<String, Object> masp = new HashMap<>();
        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        Object key = map.getKey();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("cm_name", key);
        }
        Page<Commodity> page = new PageUtile<Commodity>().getPage(map);
        baseMapper.selectPage(page, wrapper);
        List<Commodity> list = page.getRecords();

        masp.put("list", list);
        masp.put("pages", page.getPages());
        masp.put("size", page.getSize());
        masp.put("page", page.getCurrent());
        masp.put("total", page.getTotal());
        return masp;
    }

    @Override
    public Map<String, Object> getList(MapUtile mapUtile, String scName) {
        Map<String, Object> masp = new HashMap<>();
        QueryWrapper<Commodity> wrapper=new QueryWrapper<>();
        QueryWrapper<Sort> sortQueryWrapper=new QueryWrapper<>();
         sortQueryWrapper.eq("st_name",scName);
        Sort one = sortService.getOne(sortQueryWrapper);
        if (one!=null) {
            wrapper.eq("st_id",one.getStId());
            Page<Commodity> page = new PageUtile<Commodity>().getPage(mapUtile);
            baseMapper.selectPage(page, wrapper);
            List<Commodity> list = page.getRecords();
            masp.put("list", list);
            masp.put("pages", page.getPages());
            masp.put("size", page.getSize());
            masp.put("page", page.getCurrent());
            masp.put("total", page.getTotal());
        }
        return masp;
    }
}
